package com.bocai.kelalacar.ui.driver.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.PutDriverBean;
import com.bocai.kelalacar.bean.TravelInformationBean;
import com.bocai.kelalacar.bean.UploadImgBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.act.AddCarActivity;
import com.bocai.kelalacar.ui.car.set.ada.PhotoAda;
import com.bocai.kelalacar.ui.driver.set.contract.DriverCertificationContract;
import com.bocai.kelalacar.ui.driver.set.module.DriverCertificationModule;
import com.bocai.kelalacar.ui.driver.set.presenter.DriverCertificationPresenter;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.CarNumDialog;
import com.bocai.kelalacar.widget.MyGridView;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;

/**
 * Created by yuanfei on 2017/6/26.
 */

public class DriverCertificationInformationSecondActivity extends BaseActivity implements DriverCertificationContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ed_license_plate)
    EditText edLicensePlate;
    @BindView(R.id.ed_car_age)
    TextView edCarAge;
    @BindView(R.id.ll_car_age)
    LinearLayout llCarAge;
    @BindView(R.id.add_car_num)
    TextView addCarNum;
    @BindView(R.id.gv_photo)
    MyGridView gvPhoto;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    PhotoAda ada;
    ArrayList<String> photos = new ArrayList<>();
    @Inject DriverCertificationPresenter presenter;
    private PutDriverBean bean = new PutDriverBean();
    CarNumDialog carNumDialog;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    TimePickerView pickerView;

    @Override
    public int getLayoutId() {
        return R.layout.act_driver_cer_infor_second;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new DriverCertificationModule(this)).inject(this);
    }

    @Override
    public void initView() {
        bean = (PutDriverBean) getIntent().getSerializableExtra("data");
        initToolbar("认证信息");
        initBack(imgBack,llBack);
        ada = new PhotoAda(this, photos);
        gvPhoto.setAdapter(ada);
        gvPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (photos.size() < 6) {
                    PhotoPicker.builder()
                            .setPhotoCount(6 - photos.size())
                            .setShowCamera(true)
                            .setShowGif(false)
                            .setPreviewEnabled(false)
                            .start(DriverCertificationInformationSecondActivity.this, PhotoPicker.REQUEST_CODE);
                }
            }
        });
        presenter.optionalSeats();
        try {
            setDataView();
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }
    @OnClick({R.id.btn_confirm,R.id.add_car_num,R.id.ll_car_age})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_confirm:
                if (TextUtils.isEmpty(edLicensePlate.getText().toString())){
                    showShortToast("请输入车牌号");
                }else if (TextUtils.isEmpty(edCarAge.getText().toString())){
                    showShortToast("请输入车龄");
                }else if (TextUtils.isEmpty(addCarNum.getText().toString())){
                    showShortToast("请选择车座位数");
                }else if (photos == null||photos.size() == 0){
                    showShortToast("请选择车辆照片");
                }else {
                    presenter.uploadImg(photos);
                }
                break;
            case R.id.add_car_num:
                if (carNumDialog!=null){
                    carNumDialog.show();
                }
                break;
            case R.id.ll_car_age:
                pickerView.show();
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                photos = ada.getPhotos();
                List<String> list = new ArrayList<>();
                list = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                photos.addAll(list);
                ada.setPhotos(photos);
            }
        }
    }

    @Override
    public void putDriverInforSuccess() {
        showErrorTip("已提交审核请耐心等待");
        SP.put(this,"ApprovalStatus",1);
        setResult(6);
        finish();
    }

    public void setDataView() throws ParseException {
        // 指定一个日期
        pickerView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                Date date1 = new Date();

                Log.e("Date",date1.getYear()+"     "+date.getYear());

                if (date1.getYear() < (date.getYear())){
                    showErrorTip("选择时间晚于当前时间");
                }else {
                    edCarAge.setText((1900 + date.getYear()) + "");
                }

            }
        })
                .setType(TimePickerView.Type.YEAR_MONTH_DAY)
                .build();
//        pickerView.show();
    }

    @Override
    public void uploadImgSuccess(UploadImgBean uploadImgBean) {

        bean.setCarLicense(edLicensePlate.getText().toString());
        bean.setBuyTime(edCarAge.getText().toString());
        bean.setSeats(Integer.parseInt(addCarNum.getText().toString()));
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0;i<uploadImgBean.getContent().size();i++){
            if (i == uploadImgBean.getContent().size() - 1){
                stringBuilder.append(uploadImgBean.getContent().get(i).getId());
            }else {
                stringBuilder.append(uploadImgBean.getContent().get(i).getId()+",");
            }
        }
        bean.setCarImageIds(stringBuilder.toString());
        presenter.putDriverInfor(SP.get(this,"id","").toString(),bean);

    }

    @Override
    public void getTravelSuccess(TravelInformationBean bean) {

    }

    @Override
    public void optionalSeatsList(final List<Integer> list) {

        carNumDialog = new CarNumDialog(this).builder().setList(list, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                addCarNum.setText(list.get(position)+"");
                carNumDialog.dismiss();
            }
        });
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
